﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.QuanLyKho
{
    public partial class PhieuYeuCau_ChiTiet : Form
    {
        public static string MaID = "0";
        public static string IdLenh = "0";
        public static string IdSanPham = "0";

        public PhieuYeuCau_ChiTiet()
        {
            InitializeComponent();
            SetForm();
            if (MaID != "0")
                LoadFormUpdate();
        }

        private void LoadFormUpdate()
        {
            // đổ dữ liệu lên form theo lệnh và sản phẩm

            var dt = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
               "PhieuYeuCau.[PhongBan],PhieuYeuCau.[NguoiYeuCau],PhongMau_SanPham.[SoLuong],PhongMau_SanPham.[TuNgay],PhongMau_SanPham.[DenNgay],PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[NgayLL],PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],PhieuYeuCau.[ID],PhieuYeuCau.[IdSanPham],PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong] as 'SoLuongHuy',PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[SoLuong],PhieuYeuCau.[LoaiPhieu]",
               "WHERE PhieuYeuCau.ID =" + MaID);
            dtNgayLapLenh.Value = Convert.ToDateTime(dt.Rows[0]["NgayLL"]);
            txtSoLuongYeuCau.Text = dt.Rows[0]["SoLuongHuy"].ToString();
            txtSoLuong.Text = dt.Rows[0]["SoLuong"].ToString();
            txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
            dtNgayChungTu.Value = Convert.ToDateTime(dt.Rows[0]["NgayChungTu"]);
            dtNgayHachToan.Value = Convert.ToDateTime(dt.Rows[0]["NgayHoachToan"]);
            txtChungtu.Text = dt.Rows[0]["SoChungTu"].ToString();
            dtSXTuNgay.Value = Convert.ToDateTime(dt.Rows[0]["TuNgay"]);
            dtSXDenNgay.Value = Convert.ToDateTime(dt.Rows[0]["DenNgay"]);
            IdSanPham = dt.Rows[0]["IdSanPham"].ToString();
            txtMaSP.Text = dt.Rows[0]["MaSanPham"].ToString();
            txtLoaiHang.Text = dt.Rows[0]["LoaiHang"].ToString();
            txtLanTai.Text = dt.Rows[0]["LanSanXuat"].ToString();
            txtSoLenh.Text = dt.Rows[0]["LenhSanXuat"].ToString();
            luPhieuYeuCau.EditValue = dt.Rows[0]["LoaiPhieu"];
            txtNguoiYeuCau.Text = dt.Rows[0]["NguoiYeuCau"].ToString();
            txtKhoYeuCau.Text = Common.HienThiPhongBan(Convert.ToInt32(dt.Rows[0]["PhongBan"].ToString()));

            DataTable dtPycct = DataAll.SelectAll("PhieuYeuCau_ChiTiet inner join Kho_VatTu on PhieuYeuCau_ChiTiet.IDVatTu = Kho_VatTu.IDVatTu", "PhieuYeuCau_ChiTiet.[ID],PhieuYeuCau_ChiTiet.GhiChu, PhieuYeuCau_ChiTiet.SoLuong,PhieuYeuCau_ChiTiet.IDVatTu as 'Mã vật tư',Kho_VatTu.MaVatTu,Kho_VatTu.TenVatTu,Kho_VatTu.DonVi", "Where IDPhieuYeuCau = " + MaID);

            gridChiTiet.DataSource = null;
            gridChiTiet.DataSource = dtPycct;


            gcTenVatTu.OptionsColumn.AllowFocus = false;
            gcDonVi.OptionsColumn.AllowFocus = false;

            LoadImage();
            if (dt.Rows[0]["TrangThai"].ToString() == Convert.ToInt32(TrangThai.PhieuYeuCau_TaoMoi).ToString())
            {
                (gridChiTiet.MainView as GridView).AddNewRow();
            }
            else
            {
                gvChiTiet.OptionsBehavior.Editable = false;
                gcMaVatTu.OptionsColumn.AllowFocus = false;
                gcSoLuong.OptionsColumn.AllowFocus = false;
                gcGhiChu.OptionsColumn.AllowFocus = false;
                luPhieuYeuCau.Enabled = false;
               
            }
        }

        private void SetForm()
        {
            // Loại phiếu
            DataTable dtLoaiPhieuYeuCau = new DataTable("dsSiNO");
            dtLoaiPhieuYeuCau.Columns.Add("LoaiPhieu", typeof(string));
            dtLoaiPhieuYeuCau.Columns.Add("Value", typeof(int));
            dtLoaiPhieuYeuCau.Rows.Add(Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(LoaiPhieuYeuCau.CapThem)), Convert.ToInt32(LoaiPhieuYeuCau.CapThem));
            dtLoaiPhieuYeuCau.Rows.Add(Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(LoaiPhieuYeuCau.TraLai)), Convert.ToInt32(LoaiPhieuYeuCau.TraLai));

            luPhieuYeuCau.Properties.DataSource = dtLoaiPhieuYeuCau;
            luPhieuYeuCau.Properties.Columns.Add(new LookUpColumnInfo("Value"));
            luPhieuYeuCau.Properties.Columns.Add(new LookUpColumnInfo("LoaiPhieu"));
            luPhieuYeuCau.Properties.DisplayMember = "LoaiPhieu";
            luPhieuYeuCau.Properties.ValueMember = "Value";
            luPhieuYeuCau.Properties.Columns["Value"].Visible = false;
            luPhieuYeuCau.Properties.NullText = "--- Chọn loại phiếu ---";
            luPhieuYeuCau.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            luPhieuYeuCau.Properties.ShowHeader = false;

            // Vật tư trên lưới
            luMaVatTu.Properties.DataSource = DataAll.SelectAll("Kho_VatTu", "IDVatTu,MaVatTu,TenVatTu,DonVi", "");
            luMaVatTu.Properties.Columns.Add(new LookUpColumnInfo("MaVatTu"));
            luMaVatTu.Properties.Columns.Add(new LookUpColumnInfo("IDVatTu"));
            luMaVatTu.Properties.Columns.Add(new LookUpColumnInfo("TenVatTu"));
            luMaVatTu.Properties.Columns.Add(new LookUpColumnInfo("DonVi"));
            luMaVatTu.Properties.DisplayMember = "MaVatTu";
            luMaVatTu.Properties.ValueMember = "IDVatTu";
            luMaVatTu.Properties.NullText = "--- Chọn vật tư ---";
            luMaVatTu.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            luMaVatTu.Properties.ShowHeader = false;
            luMaVatTu.Properties.Columns["IDVatTu"].Visible = false;
            luMaVatTu.Properties.Columns["DonVi"].Visible = false;

            gcSoLuong.DisplayFormat.FormatType = FormatType.Numeric;
            gcSoLuong.DisplayFormat.FormatString = "{0:0,0.00}";

            gcID.Visible = false;
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh='" + txtMaSP.Text.Trim() + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"]; if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void pbAnh_Click(object sender, EventArgs e)
        {
            XemAnh.MaID = txtMaSP.Text;
            var anh = new XemAnh();
            anh.ShowDialog();
            XemAnh.MaID = "0";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
